mui.init();

mui.plusReady(function() {

	var app = new Vue({
		el: '#popover',
		data: {
			types: ''
		}
	})
	mui.ajax('http://' + ip + '/xiaojv/Api/index.php?p=front&m=upload&a=getType', {
		data: {

		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			//						console.log(data)
			app.types = data;
		},
		error: function(xhr, type, errorThrown) {
			console.log(type)
		}
	});

	document.querySelector('.backup').addEventListener('tap', function() {
		mui.back()
	})
	document.querySelector('.selectDate').addEventListener('click', function() {
		var dDate = new Date();
		var minDate = new Date();
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var y = d.getFullYear()
			var m = d.getMonth() + 1
			var d = d.getDate()
			var dTime = new Date();
			dTime.setHours(6, 0);
			plus.nativeUI.pickTime(function(e) {
				var t = e.date;
				var h = t.getHours()
				if(h == 00) {
					h = 24
				}
				var mi = t.getMinutes()
				//						console.log(mi)
				var date = y + '/' + m + '/' + d + '/' + h + ':' + mi + ':' + 0
				var sdate = y + '-' + m + '-' + d + '-' + h + ':' + mi + ':' + 0
				var ldate = y + '-' + m + '-' + d + '-' + (h - 1) + ':' + mi + ':' + 0
				var dat = new Date(sdate).getTime() / 1000
				var ldat = new Date(ldate).getTime() / 1000
				document.querySelector('.selectDate').setAttribute('data-time', sdate)
				document.querySelector('.selectDate').setAttribute('data-ltime', ldate)
				//						console.log(sdate)
				document.querySelector('.selectDate').innerText = sdate

			}, function(e) {

			}, {
				title: "请选择时间",
				is24Hour: true,
				time: dTime
			});
		}, function(e) {

		}, {
			title: "请选择日期",
			date: dDate,
			minDate: minDate
		});
	})

	var filePath

	mui('.mui-popover').on('tap', '.mui-table-view-cell', function() {
		var text = this.childNodes[0].innerText;
		var textid = this.childNodes[0].dataset.size
		document.querySelector('.selectType').innerText = text
		document.querySelector('.selectType').setAttribute('data-type', text)
		document.querySelector('.selectType').setAttribute('data-size', textid)
		mui('.mui-popover').popover('hide');
		//	  console.log()
	})

	var imgup = document.querySelector('.imgup')
	var imgs = document.querySelector('.upload_img')
	document.querySelector('.upload_img').addEventListener('tap', function() {
		if(mui.os.plus) {
			plus.nativeUI.actionSheet({
				cancel: "取消",
				buttons: [{
					title: "拍照"
				}, {
					title: "打开相册"
				}]
			}, function(event) {
				//返回的索引值 0 1 2
				var index = event.index;
				switch(index) {
					case 1:
						appendByCamera();
						break;
					case 2:
						appendByGallery();
						break;
				}
			});
		}
	})

	function appendByCamera() {
		var cmr = plus.camera.getCamera();
		//字符串数组，摄像头支持的摄像分辨率
		var res = cmr.supportedVideoResolutions[0];
		//字符串数组，摄像头支持的摄像文件格式
		var fmt = cmr.supportedVideoFormats[0];

		//录制视频开始方法
		cmr.captureImage(function(path) {
				//解析路径地址
				plus.io.resolveLocalFileSystemURL(path, function(entry) {
					filePath = entry.toLocalURL();

					imgup.src = filePath
				}, function(e) {

				});

			},
			function(error) {
				//				mui.toast("Capture video failed: " + error.message);
			}, {
				filename: '_doc/camera/',
				resolution: res,
				format: fmt,
				index: 1
			}
		);
	}

	function appendByGallery() {
		plus.gallery.pick(function(file) {
			filePath = file;
			imgup.style.display = 'none'
			imgs.style.backgroundImage = 'url(' + file + ')'
		}, function(error) {
			// Handle error
			var code = error.code; // 错误编码
			var message = error.message; // 错误描述信息
		}, {
			filter: "img"
		});
	}

	var old_back = mui.back
	mui.back = function() {
		mui.currentWebview.close();
		old_back()
	}

	var sex = 0;
	mui('.sex').on('tap', '.mui-input-row', function() {
		sex = this.childNodes[3].value
	})
	document.querySelector('.submit').addEventListener('click', function() {

		var dataset = {
			title: document.querySelector('.title_input').value,
			content: document.querySelector('.textcontent').value,
			sex: sex,
			tel: document.querySelector('[name=tel]').value,
			date: document.querySelector('.selectDate').dataset.time,
			ldate: document.querySelector('.selectDate').dataset.ltime,
			type: document.querySelector('.selectType').dataset.size,
			pro: document.querySelector('.pero').value
		};

		if(dataset.title != '' && dataset.title != null && dataset.title != undefined) {

			if(dataset.content != '' && dataset.content != null && dataset.content != undefined) {

				if(dataset.tel != '' && dataset.tel != null && dataset.tel != undefined) {

					if(dataset.date != '' && dataset.date != null && dataset.date != undefined) {

						if(dataset.type != '' && dataset.type != null && dataset.type != undefined) {

							if(dataset.pro != '' && dataset.pro != null && dataset.pro != undefined) {
								//																console.log(dataset.content)

								if(filePath != null) {

									var task = plus.uploader.createUpload('http://' + ip + '/xiaojv/Api/index.php?m=upload&p=front&a=uploadimg', {
										method: "POST"
									}, function(t, status) {
										//										console.log(t.responseText);
										//成功处理判断
										if(status == 200) {
											//把数据转换成json
											var obj = JSON.parse(t.responseText);
											//											console.log(obj.code)
											if(obj.msg && obj.code == 200) {
												mui.toast("发布成功");
												mui.back();
											} else {
												mui.toast("发布失败");
											}
										} else {
											mui.toast("发布失败");
										}
									});

									task.addData('title', dataset.title);
									task.addData('content', dataset.content);
									task.addData('sex', dataset.sex);
									task.addData('tel', dataset.tel);
									task.addData('date', dataset.date);
									task.addData('ldate', dataset.ldate);
									task.addData('type', dataset.type);
									task.addData('pro', dataset.pro);

									task.addFile(filePath, {
										key: "img"
									});

									task.start();
								} else {
									mui.ajax('http://' + ip + '/xiaojv/Api/index.php?m=upload&p=front&a=upload', {
										data: {
											data: dataset
										},
										dataType: 'json', //服务器返回json格式数据
										type: 'post', //HTTP请求类型
										timeout: 10000, //超时时间设置为10秒；
										success: function(data) {
											//											console.log(data.code)
											if(data.code == 200) {
												mui.toast(data.msg);
												mui.back()
											} else {
												mui.toast(data.msg)

											}
										},
										error: function(xhr, type, errorThrown) {
											console.log(type)
										}
									});
								}

							} else {
								mui.toast('请填写活动人数')
							}
						} else {
							mui.toast('请选择活动分类')
						}
					} else {
						mui.toast('请选择活动时间')
					}
				} else {
					mui.toast('请填写联系方式')
				}
			} else {
				mui.toast('请填写活动内容')
			}
		} else {
			mui.toast('请填写标题')
		}
	})

})